$(document).ready(function()
    {
        var data = null;

        $('.date-picker').datepicker( {
            changeYear: true,
            changeMonth: true,
            showWeek: true,
            showButtonPanel: true,
            dateFormat: 'yy-mm',
            onClose: function(dateText, inst) {
                var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
                var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
                $(this).datepicker('setDate', new Date(year, month, 1));
            }
        });
        
        $.notif_angsuran = function(_data)
        {
            $("#SKEMA_POTONGAN tbody").html('');
            data = _data;
            for(var i = 0; i < data.data.length; i++)
            {
                var current = data.data[i];
                $("#SKEMA_POTONGAN tbody").append("<tr>\n\
                                    <td><input style='float:none; margin: 0 0 0 0; width:30px;' type='checkbox' id='angsuran_"+ current.ID_PINJAMAN +"_"+ current.ANGSURAN_KE +"' name='angsuran_"+ current.ID_PINJAMAN +"_"+ current.ANGSURAN_KE +"'/></td>\n\
                                    <td>" + current.ID_PINJAMAN + "</td>\n\
                                    <td>" + current.ANGSURAN_KE + "</td>\n\
                                    <td>" + current.CICILAN_POKOK + "</td>\n\
                                    <td>" + current.BUNGA + "</td>\n\
                                    <td>" + current.ANGSURAN + "</td>\n\
                                    <td>" + current.PIUTANG + "</td>\n\
                                    </tr>");
            }
        }

        $.get_angsuran = function()
        {
            if($("#TGL_ANGSURAN").val() != "")
                $.data_ajax('br-03/potongan/date/' + $("#TGL_ANGSURAN").val(), null, $.notif_angsuran);
        }

        $.submit_angsuran = function()
        {
            $.data_ajax('br-03/potongan/realize/', "#FORM_REALISASI", $.notif_angsuran);
        }

        $("#FORM_BUAT_POTONGAN").submit(function() {
            return false;
        });

        $.select_all = function()
        {
            if($("#SELECT_ALL").attr("checked"))
            {
                for(var i = 0; i < data.data.length; i++)
                {
                    var current = data.data[i];
                    $("#angsuran_"+ current.ID_PINJAMAN +"_"+ current.ANGSURAN_KE).attr("checked", "checked");

                }
            }
            else
                for(i = 0; i < data.data.length; i++)
                {
                    current = data.data[i];
                    $("#angsuran_"+ current.ID_PINJAMAN +"_"+ current.ANGSURAN_KE).attr("checked", false);
                }
        }

        $("#FORM_REALISASI").submit(function(){return false;});

        $("#TGL_ANGSURAN").blur($.get_angsuran);

        $("#REALISASI").click($.submit_angsuran);

        $("#SELECT_ALL").change($.select_all);
    });